<template>
    <BPTable ref='tableRef' align='center' row-key='id' :columns='columns' :api='pageApi' :buttons='buttons'>
        <template #name="{record}">
            <a-space :size="16">
                <a-image v-if="record.cover" :width="50" :src="imageUrl + record.cover"></a-image>
                <div>{{ $t(record.name) }}</div>
            </a-space>
        </template>

        <template #enable='{record}'>
            <a-tag v-if='record.enable' color='success'>{{$t('enable')}}</a-tag>
            <a-tag v-else>{{$t('disable')}}</a-tag>
        </template>
    </BPTable>

    
    <BDrawer v-model:visible='visible' :title='dialogTitle' width='800px' :confirm='submit'>
        <Edit :row="currentRow" :category="currentCategory ? currentCategory.id : 0" ref="formRef" />
    </BDrawer>
</template>

<script lang='ts' setup>
    import { pageApi, deleteApi } from '@/api/Earncoin'
    import { message, Modal } from 'ant-design-vue'
    import { ref } from 'vue'
    import { useI18nStore } from '@/store/i18n'
    import { useResourceStore } from '@/store/resource'
    import Edit from './Edit.vue';
import { localeOptionsApi } from '@/api/system/Locale'
import { parseTime, recursionFn } from '@/utils';


    const imageUrl = import.meta.env.B_NET_STATIC_URL

    const i18n = useI18nStore()
    const resourceStore = useResourceStore()

    const visible = ref(false)
    const formRef = ref()
    const dialogTitle = ref()

    const tableRef = ref()

    const currentRow = ref<any>()


    const buttons = ref<SearchButton[]>([])

    buttons.value.push({
        button : 'add',
        func : () => {
            currentRow.value = null
            dialogTitle.value = '新增'
            visible.value = true
        }
    })


    const columns = ref<Column[]>([
        {
            title : '名称',
            dataIndex : 'name',
            translate : true,
            //inSearch : true,
        },

        {
            title : '链接',
            dataIndex : 'link',
        },

        {
            title : '排序',
            dataIndex : 'sort',
        },

        {
            title : '状态',
            dataIndex : 'enable',
        },

        {
            title : '创建时间',
            dataIndex : 'createTime',
            customRender({ text }) {
                return parseTime(text)
            }
        },

        {
            title : '修改时间',
            dataIndex : 'updateTime',
            customRender({ text }) {
                return parseTime(text)
            }
        },

        {
            title : i18n.t('operation'),
            dataIndex : '$action',
            align : 'center',
            width : 150,
            $edit : (row) => {
                currentRow.value = row
                dialogTitle.value = '修改'
                visible.value = true
            },

            $delete : async (row) => {
                const res = await deleteApi(row.id)
                if(res.success) {
                    message.success(i18n.t('deleteSuccess'))
                    tableRef.value.load()
                }else {
                    message.error(res.msg)
                }
            }
        }
    ])


    async function submit() {
        const res = await formRef.value.submit()
        if(res.success) {
            tableRef.value.reload()
            visible.value = false
            message.success('保存成功')
        }else {
            Modal.error({
                title : res.msg
            })
        }
    }

    
</script>

<style lang="scss" module>
    .container{
        display: flex;
        .category{
            width: 250px;
            border-right: 1px solid $borderColor;
        }
        .table{
            flex: 1;
        }
    }
</style>